﻿using System;
using System.Collections.Generic;
using System.Text;

namespace AMS.DataAccess.Filter
{
    /// <summary>
    /// 提供string类型的字段的过滤器
    /// </summary>
    public class StringFilterField : FilterField
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="value"></param>
        public StringFilterField(string value)
            :this(value, StringOperations.Equals)
        {
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="value"></param>
        /// <param name="operation"></param>
        public StringFilterField(string value, StringOperations operation)
        {
            Value = value;
            Operation = operation;
        }

        /// <summary>
        /// 
        /// </summary>
        public string Value { get; private set; }

        /// <summary>
        /// 
        /// </summary>
        public StringOperations Operation { get; private set; }

        /// <summary>
        /// 返回形如" = 'str'"的字符串
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            if (Operation == StringOperations.IsNotNull || Operation == StringOperations.IsNull)
            {
                return SqlEnumUtils.StringOperationsToString(Operation);
            }
            else if (Value == null)
            {
                return SqlEnumUtils.StringOperationsToString(StringOperations.IsNull);
            }
            else if (Operation == StringOperations.LeftLike || Operation == StringOperations.RightLike || Operation == StringOperations.Like)
            {
                return SqlEnumUtils.FormatLike(Operation, Value);
            }
            else
            {
                return SqlEnumUtils.StringOperationsToString(Operation) + SqlStringUtils.QuotedString(Value);
            }
        }
    }
}
